import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Counter from './Counter';

class App extends Component {

  constructor(props) {
    super(props);
    this.initValues = [ 10, 20, 30 ];
    const initSum = this.initValues.reduce((a, b) => a + b, 0);
    this.state = {
      sum: initSum
    }
  }


  sum = (newValue, prevValue) => {
    const change = newValue - prevValue;
    this.setState({
      sum: this.state.sum + change
    });
  }

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <Counter onUpdate={this.sum} />
          <Counter onUpdate={this.sum} initValue={this.initValues[0]}/>
          <Counter onUpdate={this.sum} initValue={this.initValues[1]}/>
          <Counter onUpdate={this.sum} initValue={this.initValues[2]}/>
          <span>当前总和为： {this.state.sum}</span>
        </header>
      </div>
    );
  }
}

export default App;
